//
// Created by sgyt on 2019/12/21.
//
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

int main(int argc,char *argv[])
{
    int fd1 = open("dup2test1.txt",O_RDWR|O_CREAT,0777);
    if(fd1 < 0 )
    {
        perror("open error\n");
    }

    int fd2 = open("dup2test2.txt",O_RDWR|O_CREAT,0777);
    if(fd2 < 0 )
    {
        perror("open error\n");
    }

    //调用dup2复制两个文件，
    dup2(fd1,fd2);
    printf("fd1:[%d], fd2:[%d]\n", fd1, fd2);

    //写入
    write(fd1,"hello",sizeof("hello"));

    printf("sizeof = %d,strlen = %d\n",sizeof("hello"),strlen("hello"));

    //调用文件描述fd1到文件初始位置,读fd2的数据
    lseek(fd1,0,SEEK_SET);

    //读取fd2的文件
    char buf[1024];
    memset(buf, sizeof(buf),0x00);
    int res = read(fd2,buf, sizeof(buf));
    printf("res = %d,buf = %s\n",res,buf);
    return 1;
}
